import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.Font;

public class SplashIIMIDlet extends MIDlet implements MenuManager
{
	public Displayable current;
	public int currentForm;
	public int prevForm;
	public String ssid;
	private SplashActionHandler actHand;
	public String ip;
	private int mainSelected;
	
	public SplashIIMIDlet()
	{	
		this.actHand = new SplashActionHandler(this);
		this.actHand.start();
		this.currentForm = -1;
		this.mainSelected = 0;
		//setScreen(new SplashFileList(this), 0);
		setScreen(loginForm(), FormConstants.LOGIN_FORM);
	}
		
	public void itemSelected(int itemId)
	{
		System.out.println("Izbran element " + itemId + " na formi " + currentForm);
		System.out.println(actHand.setAction(itemId));
		System.out.println("Method return");
	}
	
	public Displayable shutdownForm()
	{
		this.prevForm = this.currentForm;
		final String[] types = {"Zaustavitev", "Ponovni zagon", "Preklic"};
		
		SplashForm mf = new SplashForm(this, "Zaustavitev");
		mf.append(new ChoiceGroup("Tip: ", Choice.EXCLUSIVE, types , null) );
		
		mf.addCommand(new Command("Zaustavi", Command.OK, 1));
		mf.addCommand(new Command("Nazaj", Command.BACK, 1));
		
		return mf;
	}
	
	public Displayable executeForm()
	{
		this.prevForm = this.currentForm;
		
		SplashForm mf = new SplashForm(this, "Izvedi ukaz");
		mf.append(new TextField("Ukaz: ", null, 50, TextField.ANY));
		
		mf.addCommand(new Command("Poslji", Command.OK, 1));
		mf.addCommand(new Command("Nazaj", Command.BACK, 1));
		
		return mf;
	}
	
	public Displayable messageForm()
	{
		this.prevForm = this.currentForm;
		final String[] types = {"Sporocilo", "Opozorilo", "Napaka"};
		
		SplashForm mf = new SplashForm(this, "Poslji sporocilo");
		mf.append(new TextField("Sporocilo: ", null, 50, TextField.ANY));
		mf.append(new TextField("Naslov: ", null, 20, TextField.ANY));
		mf.append(new ChoiceGroup("Tip: ", Choice.EXCLUSIVE, types , null) );
		
		mf.addCommand(new Command("Poslji", Command.OK, 1));
		mf.addCommand(new Command("Nazaj", Command.BACK, 1));
		
		return mf;
	}
	
	public Displayable loginForm()
	{
		this.prevForm = this.currentForm;
		SplashForm f = new SplashForm(this, "Prijava");
		f.append(new StringItem("","Prijava"));
		f.append(new TextField("IP:", this.ip, 20, TextField.ANY));
		f.append(new TextField("Geslo:", null, 30, TextField.PASSWORD));
		
		Form ths = (Form)f;
		((StringItem)ths.get(0)).setFont(Font.getFont(Font.FACE_PROPORTIONAL,
				Font.STYLE_BOLD | Font.STYLE_UNDERLINED, Font.SIZE_LARGE));
		ths.get(0).setLayout(Item.LAYOUT_CENTER);
		ths.get(1).setLayout(Item.LAYOUT_CENTER | Item.LAYOUT_EXPAND);
		ths.get(2).setLayout(Item.LAYOUT_CENTER | Item.LAYOUT_EXPAND);
		
		f.addCommand(new Command("Prijava", Command.OK, 1));
		f.addCommand(new Command("Izhod", Command.EXIT, 1));

		return f;
	}
	
	public Displayable mainMenu()
	{
		this.prevForm = this.currentForm;
		/* Application started. Load menu */
		MenuItem[] list = new MenuItem[10];
		try
		{
			list[0] = new MenuItem("Sporocilo", Image.createImage("/message.png"));
			list[1] = new MenuItem("Ugasni racunalnik", Image.createImage("/shutdown.png"));
			list[2] = new MenuItem("Pozeni program", Image.createImage("/run.png"));
			list[3] = new MenuItem("Slika zaslona", Image.createImage("/screen.png"));
			list[4] = new MenuItem("Ustavi streznik", Image.createImage("/stop.png"));
			list[5] = new MenuItem("Predstavitveni nacin", Image.createImage("/presentation.png"));
			list[6] = new MenuItem("Miska", Image.createImage("/mouse.png"));
			list[7] = new MenuItem("Datoteke", Image.createImage("/files.png"));
			list[8] = new MenuItem("Izhod", Image.createImage("/shutdown.png"));
			list[9] = new MenuItem("Info", Image.createImage("/about.png"));
		}
		catch(IOException e)
		{
			/* Resource not found handling */
			System.out.println("Ne morem naloziti slik.");
			super.notifyDestroyed();
		}
		
		return new SplashMenu(list, this);
	}
	
	public void setScreen(Displayable d, int curr)
	{
		this.prevForm = this.currentForm;
		this.currentForm = curr;
		this.current = d;
		System.out.println(d);
		Display.getDisplay(this).setCurrent(d);
	}
	
	public String urlEncode(String plain)
	{
		String r = "";
		int len = plain.length();
		for(int i = 0; i < len; i++)
		{
			char ch = plain.charAt(i);
			if((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || 
			(ch >= '0' && ch <= '9') || ch == '.' || ch == '-' || ch == '*' || ch == '_')
			{
				r += ch;
			}
			else if(ch == ' ')
			{
				r += '+';
			}
			else
			{
				r += '%';
				/* <16? */
				if(ch <= 16)
					r += '0' + Integer.toHexString((int)ch);
				else
					r += Integer.toHexString((int)ch);
			}
		}
		return r;
	}
	
	public String urlDecode(String plain)
	{
		String r = "";
		int len = plain.length();
		for(int i = 0; i < len; i++)
		{
			char ch = plain.charAt(i);
			if(ch == '+')
			{
				r += ' ';
			}
			else if(ch == '%')
			{
				char fr = plain.charAt(i++);
				char sc = plain.charAt(i++);
				char sum = 0;
				if(fr >= '0' )
					fr -= '0';
				else if(fr >= 'A')
					fr -= 'A';
				else
					fr -= 'a';
				sum += fr * 16;
				
				if(sc >= '0' )
					sc -= '0';
				else if(sc >= 'A')
					sc -= 'A';
				else
					sc -= 'a';
				sum += sc * 256;
				
				r += sum;
			}
		}
		return r;
	}
	
	public void mainSelected(int ms)
	{
		this.mainSelected = ms;
	}
	
	public int getMainSelected()
	{
		return this.mainSelected;
	}
	
	public int getDefaultBGColor()
	{
		return 0;
	}
	
	public void startApp()
	{
	}
	
	public void pauseApp()
	{
	}
	
	public void destroyApp(boolean unconditional)
	{	
	}
}
